﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace RapChieuPhim.Modules
{
    public partial class InfoAllAgent : UserControl
    {
        private int _MaRap = Modules.Login.user.NhanVien.Rap.MaRap;
        int _MaCV = Modules.Login.user.NhanVien.ChucVu.MaCV;
        int _landau = 0;

        public InfoAllAgent()
        {
            InitializeComponent();
        }

        private void LoadData()
        {
            if (Modules.Login.user.NhanVien.ChucVu.MaCV == 0)
                gridControlMain.DataSource = BUS.NhanVienBUS.GetAll();
            else if (Modules.Login.user.NhanVien.ChucVu.MaCV == 1)
                gridControlMain.DataSource = BUS.NhanVienBUS.GetAllByMaRap(_MaRap);
            lookChucVu.Properties.DataSource = BUS.ChucVuBUS.GetAll();
            if (_landau==0)
            {
                lookChucVu.Properties.Columns.Add(new DevExpress.XtraEditors.Controls.LookUpColumnInfo("MaCV", 30, "Mã"));
                lookChucVu.Properties.Columns.Add(new DevExpress.XtraEditors.Controls.LookUpColumnInfo("TenCV", 180, "Chức vụ"));
            }
            lookChucVu.Properties.DisplayMember = "TenCV";
            lookChucVu.Properties.ValueMember = "MaCV";
            lookChucVu.Properties.PopupFormMinSize = new Size(210, 100);
            _landau++;
        }

        private void InfoAllAgent_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void BingdingData(DTO.NhanVienDTO nhanvien)
        {
            txtName.ResetText();
        }

        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (gridView1.FocusedRowHandle < 0 || gridView1.FocusedRowHandle >= gridView1.RowCount)
                return;
            string _Value = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, colMaNV).ToString();
            DTO.NhanVienDTO _NhanVien = BUS.NhanVienBUS.GetByID(_Value);
            BindingData(_NhanVien);
        }

        private void BindingData(DTO.NhanVienDTO _NhanVien)
        {
            txtName.Text = _NhanVien.HoTen;
            txtMaNV.Text = _NhanVien.MaNhanVien;
            if (_NhanVien.GioiTinh)
                radioNam.Checked = true;
            else radioNu.Checked = true;
            txtDiaChi.Text = _NhanVien.DiaChi;
            txtCMND.Text = _NhanVien.CMND;
            txtDienThoai.Text = _NhanVien.DienThoai;
            dateNgaySinh.EditValue = _NhanVien.NgaySinh;
            lookChucVu.EditValue = _NhanVien.ChucVu.MaCV;
        }

        private void gridView1_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {

        }

        private void navBarItem4_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            if (dxValidationProvider1.Validate())
            {
                if (XtraMessageBox.Show("Thêm nhân viên này ?", null, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    string _LastID = BUS.NhanVienBUS.LayLastNhanVien();
                    string _NextID = Util.ObjectUtil.NextID(_LastID, "NV", 4);
                    DTO.NhanVienDTO _NhanVien = new DTO.NhanVienDTO();
                    _NhanVien.MaNhanVien = _NextID;
                    _NhanVien.HoTen = txtName.Text;
                    _NhanVien.NgaySinh = DateTime.Parse(dateNgaySinh.EditValue.ToString());
                    if (radioNam.Checked)
                        _NhanVien.GioiTinh = true;
                    else
                        _NhanVien.GioiTinh = false;
                    _NhanVien.DienThoai = txtDienThoai.Text;
                    _NhanVien.DiaChi = txtDiaChi.Text;
                    _NhanVien.CMND = txtCMND.Text;
                    _NhanVien.ChucVu = BUS.ChucVuBUS.GetByID(int.Parse(lookChucVu.EditValue.ToString()));
                    BUS.NhanVienBUS.Insert(_NhanVien, _MaRap);
                    LoadData();
                    gridView1.FocusedRowHandle = gridView1.RowCount - 1;
                    XtraMessageBox.Show("Thêm nhân viên thành công", null, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }

        private void navBarItem5_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            string _MaNV = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, colMaNV).ToString();
            if (_MaNV == "NV0000")
                XtraMessageBox.Show("Bạn không được xóa quản trị viên !", null, MessageBoxButtons.OK, MessageBoxIcon.Error);
            else
            {
                if (XtraMessageBox.Show("Xóa thông tin nhân viên này ?", null, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    BUS.NhanVienBUS.Delete(_MaNV);
                    LoadData();
                    XtraMessageBox.Show("Thao tác thành công !", null, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }            
        }

        private void navBarItem6_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            string _MaNV = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, colMaNV).ToString();
            if (_MaNV == "NV0000")
                XtraMessageBox.Show("Bạn không được chỉnh sửa thông tin quản trị viên !", null, MessageBoxButtons.OK, MessageBoxIcon.Error);
            else
            {
                if (dxValidationProvider1.Validate())
                {
                    if (XtraMessageBox.Show("Sửa thông tin nhân viên này ?", null, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        DTO.NhanVienDTO _NhanVien = new DTO.NhanVienDTO();
                        _NhanVien.MaNhanVien = _MaNV;
                        _NhanVien.HoTen = txtName.Text;
                        _NhanVien.NgaySinh = DateTime.Parse(dateNgaySinh.EditValue.ToString());
                        if (radioNam.Checked)
                            _NhanVien.GioiTinh = true;
                        else
                            _NhanVien.GioiTinh = false;
                        _NhanVien.DienThoai = txtDienThoai.Text;
                        _NhanVien.DiaChi = txtDiaChi.Text;
                        _NhanVien.CMND = txtCMND.Text;
                        _NhanVien.ChucVu = BUS.ChucVuBUS.GetByID(int.Parse(lookChucVu.EditValue.ToString()));
                        BUS.NhanVienBUS.Update(_NhanVien, _MaRap);
                        LoadData();
                        gridView1.FocusedRowHandle = gridView1.RowCount - 1;
                        XtraMessageBox.Show("Sửa nhân viên thành công", null, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }            
        }

        private void navBarItem2_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            gridControlMain.ShowPrintPreview();
        }

        private void navBarItem8_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            SaveFileDialog _dialog = new SaveFileDialog();
            _dialog.InitialDirectory = Application.StartupPath.ToString();
            _dialog.Title = "Xuất ra file pdf";
            _dialog.DefaultExt = "pdf";
            _dialog.Filter = "All files (*.*)|*.*";
            _dialog.FilterIndex = 0;
            _dialog.RestoreDirectory = true;
            if (_dialog.ShowDialog() == DialogResult.OK)
            {
                gridControlMain.ExportToPdf(_dialog.FileName.ToString());

                System.Diagnostics.Process _prc = new System.Diagnostics.Process();
                _prc.StartInfo.FileName = System.IO.Path.GetDirectoryName(_dialog.FileName);
                _prc.Start();
            }
        }
    }
}
